#!/bin/sh
# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use pw.x for a charge self-consistent DFT+DMFT"
$ECHO "calculation by incorporating the occupation updates"
$ECHO

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST="Sr.pbesol-spn-rrkjus_psl.1.0.0.UPF V.pbesol-spnl-rrkjus_psl.1.0.0.UPF O.pbesol-n-rrkjus_psl.0.1.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x     as: $PW_COMMAND"
$ECHO


# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

PREFIX='srvo3'

# self-consistent calculation
cat > $PREFIX.scf.in << EOF
 &control
    calculation        = 'scf',
    prefix             = '$PREFIX',
    tstress            = .true.,
    tprnfor            = .true.,
    pseudo_dir         = '$PSEUDO_DIR/',
    outdir             = '$TMP_DIR/',
 /
 &system
    ibrav              = 1,
    celldm(1)          = 7.2612,
    nat                = 5,
    ntyp               = 3,
    ecutwfc            = 60.0,
    ecutrho            = 720.0,
    occupations        = 'smearing',
    degauss            = 0.02,
    smearing           = 'm-p',
 /
 &electrons
    mixing_beta        = 0.7,
    mixing_mode        = 'local-TF'
 /

 ATOMIC_SPECIES
    Sr	87.62		Sr.pbesol-spn-rrkjus_psl.1.0.0.UPF
    V	50.9415		V.pbesol-spnl-rrkjus_psl.1.0.0.UPF
    O	15.9994		O.pbesol-n-rrkjus_psl.0.1.UPF

 ATOMIC_POSITIONS (crystal)
 Sr       0.000000000   0.000000000   0.000000000
 V        0.500000000   0.500000000   0.500000000
 O        0.500000000   0.500000000   0.000000000
 O        0.500000000   0.000000000   0.500000000
 O        0.000000000   0.500000000   0.500000000

 K_POINTS automatic
 5 5 5 0 0 0
EOF
$ECHO "  Running the SCF calculation for $PREFIX..."
$PW_COMMAND < $PREFIX.scf.in > $PREFIX.scf.out
check_failure $?
$ECHO " done"

# non-self-consistent calculation
cat > $PREFIX.nscf.in << EOF
 &control
    calculation        = 'nscf',
    prefix             = '$PREFIX',
    tstress            = .true.,
    tprnfor            = .true.,
    verbosity          = 'high',
    pseudo_dir         = '$PSEUDO_DIR/',
    outdir             = '$TMP_DIR/',
 /
 &system
    ibrav              = 1,
    celldm(1)          = 7.2612,
    nat                = 5,
    ntyp               = 3,
    ecutwfc            = 60.0,
    ecutrho            = 720.0,
    occupations        = 'smearing',
    degauss            = 0.02,
    smearing           = 'm-p',
    dmft               = .true.,
    nosym              = .true.,
 /
 &electrons
 /

 ATOMIC_SPECIES
    Sr	87.62		Sr.pbesol-spn-rrkjus_psl.1.0.0.UPF
    V	50.9415		V.pbesol-spnl-rrkjus_psl.1.0.0.UPF
    O	15.9994		O.pbesol-n-rrkjus_psl.0.1.UPF

 ATOMIC_POSITIONS (crystal)
 Sr       0.000000000   0.000000000   0.000000000
 V        0.500000000   0.500000000   0.500000000
 O        0.500000000   0.500000000   0.000000000
 O        0.500000000   0.000000000   0.500000000
 O        0.000000000   0.500000000   0.500000000

 K_POINTS crystal
 125
   0.00000000  0.00000000  0.00000000  8.000000e-03
   0.00000000  0.00000000  0.20000000  8.000000e-03
   0.00000000  0.00000000  0.40000000  8.000000e-03
   0.00000000  0.00000000  0.60000000  8.000000e-03
   0.00000000  0.00000000  0.80000000  8.000000e-03
   0.00000000  0.20000000  0.00000000  8.000000e-03
   0.00000000  0.20000000  0.20000000  8.000000e-03
   0.00000000  0.20000000  0.40000000  8.000000e-03
   0.00000000  0.20000000  0.60000000  8.000000e-03
   0.00000000  0.20000000  0.80000000  8.000000e-03
   0.00000000  0.40000000  0.00000000  8.000000e-03
   0.00000000  0.40000000  0.20000000  8.000000e-03
   0.00000000  0.40000000  0.40000000  8.000000e-03
   0.00000000  0.40000000  0.60000000  8.000000e-03
   0.00000000  0.40000000  0.80000000  8.000000e-03
   0.00000000  0.60000000  0.00000000  8.000000e-03
   0.00000000  0.60000000  0.20000000  8.000000e-03
   0.00000000  0.60000000  0.40000000  8.000000e-03
   0.00000000  0.60000000  0.60000000  8.000000e-03
   0.00000000  0.60000000  0.80000000  8.000000e-03
   0.00000000  0.80000000  0.00000000  8.000000e-03
   0.00000000  0.80000000  0.20000000  8.000000e-03
   0.00000000  0.80000000  0.40000000  8.000000e-03
   0.00000000  0.80000000  0.60000000  8.000000e-03
   0.00000000  0.80000000  0.80000000  8.000000e-03
   0.20000000  0.00000000  0.00000000  8.000000e-03
   0.20000000  0.00000000  0.20000000  8.000000e-03
   0.20000000  0.00000000  0.40000000  8.000000e-03
   0.20000000  0.00000000  0.60000000  8.000000e-03
   0.20000000  0.00000000  0.80000000  8.000000e-03
   0.20000000  0.20000000  0.00000000  8.000000e-03
   0.20000000  0.20000000  0.20000000  8.000000e-03
   0.20000000  0.20000000  0.40000000  8.000000e-03
   0.20000000  0.20000000  0.60000000  8.000000e-03
   0.20000000  0.20000000  0.80000000  8.000000e-03
   0.20000000  0.40000000  0.00000000  8.000000e-03
   0.20000000  0.40000000  0.20000000  8.000000e-03
   0.20000000  0.40000000  0.40000000  8.000000e-03
   0.20000000  0.40000000  0.60000000  8.000000e-03
   0.20000000  0.40000000  0.80000000  8.000000e-03
   0.20000000  0.60000000  0.00000000  8.000000e-03
   0.20000000  0.60000000  0.20000000  8.000000e-03
   0.20000000  0.60000000  0.40000000  8.000000e-03
   0.20000000  0.60000000  0.60000000  8.000000e-03
   0.20000000  0.60000000  0.80000000  8.000000e-03
   0.20000000  0.80000000  0.00000000  8.000000e-03
   0.20000000  0.80000000  0.20000000  8.000000e-03
   0.20000000  0.80000000  0.40000000  8.000000e-03
   0.20000000  0.80000000  0.60000000  8.000000e-03
   0.20000000  0.80000000  0.80000000  8.000000e-03
   0.40000000  0.00000000  0.00000000  8.000000e-03
   0.40000000  0.00000000  0.20000000  8.000000e-03
   0.40000000  0.00000000  0.40000000  8.000000e-03
   0.40000000  0.00000000  0.60000000  8.000000e-03
   0.40000000  0.00000000  0.80000000  8.000000e-03
   0.40000000  0.20000000  0.00000000  8.000000e-03
   0.40000000  0.20000000  0.20000000  8.000000e-03
   0.40000000  0.20000000  0.40000000  8.000000e-03
   0.40000000  0.20000000  0.60000000  8.000000e-03
   0.40000000  0.20000000  0.80000000  8.000000e-03
   0.40000000  0.40000000  0.00000000  8.000000e-03
   0.40000000  0.40000000  0.20000000  8.000000e-03
   0.40000000  0.40000000  0.40000000  8.000000e-03
   0.40000000  0.40000000  0.60000000  8.000000e-03
   0.40000000  0.40000000  0.80000000  8.000000e-03
   0.40000000  0.60000000  0.00000000  8.000000e-03
   0.40000000  0.60000000  0.20000000  8.000000e-03
   0.40000000  0.60000000  0.40000000  8.000000e-03
   0.40000000  0.60000000  0.60000000  8.000000e-03
   0.40000000  0.60000000  0.80000000  8.000000e-03
   0.40000000  0.80000000  0.00000000  8.000000e-03
   0.40000000  0.80000000  0.20000000  8.000000e-03
   0.40000000  0.80000000  0.40000000  8.000000e-03
   0.40000000  0.80000000  0.60000000  8.000000e-03
   0.40000000  0.80000000  0.80000000  8.000000e-03
   0.60000000  0.00000000  0.00000000  8.000000e-03
   0.60000000  0.00000000  0.20000000  8.000000e-03
   0.60000000  0.00000000  0.40000000  8.000000e-03
   0.60000000  0.00000000  0.60000000  8.000000e-03
   0.60000000  0.00000000  0.80000000  8.000000e-03
   0.60000000  0.20000000  0.00000000  8.000000e-03
   0.60000000  0.20000000  0.20000000  8.000000e-03
   0.60000000  0.20000000  0.40000000  8.000000e-03
   0.60000000  0.20000000  0.60000000  8.000000e-03
   0.60000000  0.20000000  0.80000000  8.000000e-03
   0.60000000  0.40000000  0.00000000  8.000000e-03
   0.60000000  0.40000000  0.20000000  8.000000e-03
   0.60000000  0.40000000  0.40000000  8.000000e-03
   0.60000000  0.40000000  0.60000000  8.000000e-03
   0.60000000  0.40000000  0.80000000  8.000000e-03
   0.60000000  0.60000000  0.00000000  8.000000e-03
   0.60000000  0.60000000  0.20000000  8.000000e-03
   0.60000000  0.60000000  0.40000000  8.000000e-03
   0.60000000  0.60000000  0.60000000  8.000000e-03
   0.60000000  0.60000000  0.80000000  8.000000e-03
   0.60000000  0.80000000  0.00000000  8.000000e-03
   0.60000000  0.80000000  0.20000000  8.000000e-03
   0.60000000  0.80000000  0.40000000  8.000000e-03
   0.60000000  0.80000000  0.60000000  8.000000e-03
   0.60000000  0.80000000  0.80000000  8.000000e-03
   0.80000000  0.00000000  0.00000000  8.000000e-03
   0.80000000  0.00000000  0.20000000  8.000000e-03
   0.80000000  0.00000000  0.40000000  8.000000e-03
   0.80000000  0.00000000  0.60000000  8.000000e-03
   0.80000000  0.00000000  0.80000000  8.000000e-03
   0.80000000  0.20000000  0.00000000  8.000000e-03
   0.80000000  0.20000000  0.20000000  8.000000e-03
   0.80000000  0.20000000  0.40000000  8.000000e-03
   0.80000000  0.20000000  0.60000000  8.000000e-03
   0.80000000  0.20000000  0.80000000  8.000000e-03
   0.80000000  0.40000000  0.00000000  8.000000e-03
   0.80000000  0.40000000  0.20000000  8.000000e-03
   0.80000000  0.40000000  0.40000000  8.000000e-03
   0.80000000  0.40000000  0.60000000  8.000000e-03
   0.80000000  0.40000000  0.80000000  8.000000e-03
   0.80000000  0.60000000  0.00000000  8.000000e-03
   0.80000000  0.60000000  0.20000000  8.000000e-03
   0.80000000  0.60000000  0.40000000  8.000000e-03
   0.80000000  0.60000000  0.60000000  8.000000e-03
   0.80000000  0.60000000  0.80000000  8.000000e-03
   0.80000000  0.80000000  0.00000000  8.000000e-03
   0.80000000  0.80000000  0.20000000  8.000000e-03
   0.80000000  0.80000000  0.40000000  8.000000e-03
   0.80000000  0.80000000  0.60000000  8.000000e-03
   0.80000000  0.80000000  0.80000000  8.000000e-03
EOF
$ECHO "  Running the NSCF calculation for $PREFIX..."
$PW_COMMAND < $PREFIX.nscf.in > $PREFIX.nscf.out
check_failure $?
$ECHO " done"

# modified pseudo-self-consistent calculation for dmft
cat > $PREFIX.dmft_scf.in << EOF
 &control
    calculation        = 'scf',
    prefix             = '$PREFIX',
    restart_mode       = 'restart',
    tstress            = .true.,
    tprnfor            = .true.,
    verbosity          = 'high',
    pseudo_dir         = '$PSEUDO_DIR/',
    outdir             = '$TMP_DIR/',
 /
 &system
    ibrav              = 1,
    celldm(1)          = 7.2612,
    nat                = 5,
    ntyp               = 3,
    ecutwfc            = 60.0,
    ecutrho            = 720.0,
    occupations        = 'smearing',
    degauss            = 0.02,
    smearing           = 'm-p',
    dmft               = .true.,
    dmft_prefix        = '${PREFIX}_dmft',
    nosym	           = .true.,
 /
 &electrons
    electron_maxstep   = 1,
    mixing_beta        = 0.2,
    mixing_mode        = 'local-TF'
    startingwfc        = 'file',
    startingpot        = 'file',
 /

 ATOMIC_SPECIES
    Sr	87.62		Sr.pbesol-spn-rrkjus_psl.1.0.0.UPF
    V	50.9415		V.pbesol-spnl-rrkjus_psl.1.0.0.UPF
    O	15.9994		O.pbesol-n-rrkjus_psl.0.1.UPF

 ATOMIC_POSITIONS (crystal)
 Sr       0.000000000   0.000000000   0.000000000
 V        0.500000000   0.500000000   0.500000000
 O        0.500000000   0.500000000   0.000000000
 O        0.500000000   0.000000000   0.500000000
 O        0.000000000   0.500000000   0.500000000

 K_POINTS crystal
 125
   0.00000000  0.00000000  0.00000000  8.000000e-03
   0.00000000  0.00000000  0.20000000  8.000000e-03
   0.00000000  0.00000000  0.40000000  8.000000e-03
   0.00000000  0.00000000  0.60000000  8.000000e-03
   0.00000000  0.00000000  0.80000000  8.000000e-03
   0.00000000  0.20000000  0.00000000  8.000000e-03
   0.00000000  0.20000000  0.20000000  8.000000e-03
   0.00000000  0.20000000  0.40000000  8.000000e-03
   0.00000000  0.20000000  0.60000000  8.000000e-03
   0.00000000  0.20000000  0.80000000  8.000000e-03
   0.00000000  0.40000000  0.00000000  8.000000e-03
   0.00000000  0.40000000  0.20000000  8.000000e-03
   0.00000000  0.40000000  0.40000000  8.000000e-03
   0.00000000  0.40000000  0.60000000  8.000000e-03
   0.00000000  0.40000000  0.80000000  8.000000e-03
   0.00000000  0.60000000  0.00000000  8.000000e-03
   0.00000000  0.60000000  0.20000000  8.000000e-03
   0.00000000  0.60000000  0.40000000  8.000000e-03
   0.00000000  0.60000000  0.60000000  8.000000e-03
   0.00000000  0.60000000  0.80000000  8.000000e-03
   0.00000000  0.80000000  0.00000000  8.000000e-03
   0.00000000  0.80000000  0.20000000  8.000000e-03
   0.00000000  0.80000000  0.40000000  8.000000e-03
   0.00000000  0.80000000  0.60000000  8.000000e-03
   0.00000000  0.80000000  0.80000000  8.000000e-03
   0.20000000  0.00000000  0.00000000  8.000000e-03
   0.20000000  0.00000000  0.20000000  8.000000e-03
   0.20000000  0.00000000  0.40000000  8.000000e-03
   0.20000000  0.00000000  0.60000000  8.000000e-03
   0.20000000  0.00000000  0.80000000  8.000000e-03
   0.20000000  0.20000000  0.00000000  8.000000e-03
   0.20000000  0.20000000  0.20000000  8.000000e-03
   0.20000000  0.20000000  0.40000000  8.000000e-03
   0.20000000  0.20000000  0.60000000  8.000000e-03
   0.20000000  0.20000000  0.80000000  8.000000e-03
   0.20000000  0.40000000  0.00000000  8.000000e-03
   0.20000000  0.40000000  0.20000000  8.000000e-03
   0.20000000  0.40000000  0.40000000  8.000000e-03
   0.20000000  0.40000000  0.60000000  8.000000e-03
   0.20000000  0.40000000  0.80000000  8.000000e-03
   0.20000000  0.60000000  0.00000000  8.000000e-03
   0.20000000  0.60000000  0.20000000  8.000000e-03
   0.20000000  0.60000000  0.40000000  8.000000e-03
   0.20000000  0.60000000  0.60000000  8.000000e-03
   0.20000000  0.60000000  0.80000000  8.000000e-03
   0.20000000  0.80000000  0.00000000  8.000000e-03
   0.20000000  0.80000000  0.20000000  8.000000e-03
   0.20000000  0.80000000  0.40000000  8.000000e-03
   0.20000000  0.80000000  0.60000000  8.000000e-03
   0.20000000  0.80000000  0.80000000  8.000000e-03
   0.40000000  0.00000000  0.00000000  8.000000e-03
   0.40000000  0.00000000  0.20000000  8.000000e-03
   0.40000000  0.00000000  0.40000000  8.000000e-03
   0.40000000  0.00000000  0.60000000  8.000000e-03
   0.40000000  0.00000000  0.80000000  8.000000e-03
   0.40000000  0.20000000  0.00000000  8.000000e-03
   0.40000000  0.20000000  0.20000000  8.000000e-03
   0.40000000  0.20000000  0.40000000  8.000000e-03
   0.40000000  0.20000000  0.60000000  8.000000e-03
   0.40000000  0.20000000  0.80000000  8.000000e-03
   0.40000000  0.40000000  0.00000000  8.000000e-03
   0.40000000  0.40000000  0.20000000  8.000000e-03
   0.40000000  0.40000000  0.40000000  8.000000e-03
   0.40000000  0.40000000  0.60000000  8.000000e-03
   0.40000000  0.40000000  0.80000000  8.000000e-03
   0.40000000  0.60000000  0.00000000  8.000000e-03
   0.40000000  0.60000000  0.20000000  8.000000e-03
   0.40000000  0.60000000  0.40000000  8.000000e-03
   0.40000000  0.60000000  0.60000000  8.000000e-03
   0.40000000  0.60000000  0.80000000  8.000000e-03
   0.40000000  0.80000000  0.00000000  8.000000e-03
   0.40000000  0.80000000  0.20000000  8.000000e-03
   0.40000000  0.80000000  0.40000000  8.000000e-03
   0.40000000  0.80000000  0.60000000  8.000000e-03
   0.40000000  0.80000000  0.80000000  8.000000e-03
   0.60000000  0.00000000  0.00000000  8.000000e-03
   0.60000000  0.00000000  0.20000000  8.000000e-03
   0.60000000  0.00000000  0.40000000  8.000000e-03
   0.60000000  0.00000000  0.60000000  8.000000e-03
   0.60000000  0.00000000  0.80000000  8.000000e-03
   0.60000000  0.20000000  0.00000000  8.000000e-03
   0.60000000  0.20000000  0.20000000  8.000000e-03
   0.60000000  0.20000000  0.40000000  8.000000e-03
   0.60000000  0.20000000  0.60000000  8.000000e-03
   0.60000000  0.20000000  0.80000000  8.000000e-03
   0.60000000  0.40000000  0.00000000  8.000000e-03
   0.60000000  0.40000000  0.20000000  8.000000e-03
   0.60000000  0.40000000  0.40000000  8.000000e-03
   0.60000000  0.40000000  0.60000000  8.000000e-03
   0.60000000  0.40000000  0.80000000  8.000000e-03
   0.60000000  0.60000000  0.00000000  8.000000e-03
   0.60000000  0.60000000  0.20000000  8.000000e-03
   0.60000000  0.60000000  0.40000000  8.000000e-03
   0.60000000  0.60000000  0.60000000  8.000000e-03
   0.60000000  0.60000000  0.80000000  8.000000e-03
   0.60000000  0.80000000  0.00000000  8.000000e-03
   0.60000000  0.80000000  0.20000000  8.000000e-03
   0.60000000  0.80000000  0.40000000  8.000000e-03
   0.60000000  0.80000000  0.60000000  8.000000e-03
   0.60000000  0.80000000  0.80000000  8.000000e-03
   0.80000000  0.00000000  0.00000000  8.000000e-03
   0.80000000  0.00000000  0.20000000  8.000000e-03
   0.80000000  0.00000000  0.40000000  8.000000e-03
   0.80000000  0.00000000  0.60000000  8.000000e-03
   0.80000000  0.00000000  0.80000000  8.000000e-03
   0.80000000  0.20000000  0.00000000  8.000000e-03
   0.80000000  0.20000000  0.20000000  8.000000e-03
   0.80000000  0.20000000  0.40000000  8.000000e-03
   0.80000000  0.20000000  0.60000000  8.000000e-03
   0.80000000  0.20000000  0.80000000  8.000000e-03
   0.80000000  0.40000000  0.00000000  8.000000e-03
   0.80000000  0.40000000  0.20000000  8.000000e-03
   0.80000000  0.40000000  0.40000000  8.000000e-03
   0.80000000  0.40000000  0.60000000  8.000000e-03
   0.80000000  0.40000000  0.80000000  8.000000e-03
   0.80000000  0.60000000  0.00000000  8.000000e-03
   0.80000000  0.60000000  0.20000000  8.000000e-03
   0.80000000  0.60000000  0.40000000  8.000000e-03
   0.80000000  0.60000000  0.60000000  8.000000e-03
   0.80000000  0.60000000  0.80000000  8.000000e-03
   0.80000000  0.80000000  0.00000000  8.000000e-03
   0.80000000  0.80000000  0.20000000  8.000000e-03
   0.80000000  0.80000000  0.40000000  8.000000e-03
   0.80000000  0.80000000  0.60000000  8.000000e-03
   0.80000000  0.80000000  0.80000000  8.000000e-03
EOF
cp ../${PREFIX}_dmft.h5 .
$ECHO "  Running the SCF calculation for $PREFIX..."
$PW_COMMAND < $PREFIX.dmft_scf.in > $PREFIX.dmft_scf.out
check_failure $?
$ECHO " done"

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/$PREFIX*
$ECHO " done"

$ECHO
$ECHO "$EXAMPLE_DIR: done"
